/**
 * 标签页管理器
 */
class TabManager {
    constructor() {
        this.tabButtons = document.querySelectorAll('.tab-btn');
        this.tabContents = document.querySelectorAll('.tab-content');
        this.init();
    }

    init() {
        this.tabButtons.forEach(button => {
            button.addEventListener('click', (e) => {
                this.switchTab(button.getAttribute('data-tab'));
            });
        });
    }

    switchTab(tabId) {
        // 更新按钮状态
        this.updateButtonStates(tabId);
        
        // 更新内容显示
        this.updateContentDisplay(tabId);
        
        // 处理特定标签页的逻辑
        this.handleTabSpecificLogic(tabId);
    }

    updateButtonStates(activeTabId) {
        this.tabButtons.forEach(btn => {
            const isActive = btn.getAttribute('data-tab') === activeTabId;
            
            if (isActive) {
                btn.classList.remove('text-gray-600', 'hover:text-blue-600');
                btn.classList.add('text-blue-600', 'border-b-2', 'border-blue-600');
            } else {
                btn.classList.remove('text-blue-600', 'border-b-2', 'border-blue-600');
                btn.classList.add('text-gray-600', 'hover:text-blue-600');
            }
        });
    }

    updateContentDisplay(activeTabId) {
        this.tabContents.forEach(content => {
            const isActive = content.id === `${activeTabId}-tab`;
            
            if (isActive) {
                content.classList.add('active');
            } else {
                content.classList.remove('active');
            }
        });
    }

    handleTabSpecificLogic(tabId) {
        switch (tabId) {
            case 'directories':
                // 切换到目录管理标签页时，加载目录列表
                if (window.directoryManager) {
                    window.directoryManager.loadDirectoriesList();
                }
                break;
            case 'settings':
                // 切换到系统设置标签页时，刷新系统信息
                if (window.systemManager) {
                    window.systemManager.refreshSystemInfo();
                }
                break;
        }
    }

    getActiveTab() {
        const activeButton = document.querySelector('.tab-btn.text-blue-600');
        return activeButton ? activeButton.getAttribute('data-tab') : null;
    }
}

// 创建全局标签页管理器实例
window.tabManager = new TabManager();